// 公共方法
// 引入公共参数
import {
	BASE_URL,
	DOMAIN_NAME
} from './config.js';

// 判断是否登录
export function isLogin() {
	let token = uni.getStorageSync('token');
	if (token) return true;
	return false;
}

// 登录判断跳转
export function toUrl(url, intercept = false) {
	if (intercept && !isLogin()) {
		uni.navigateTo({
			url: '/pages/login/login'
		})
	} else {
		uni.navigateTo({
			url
		})
	}
}

// 开发中提示
export function inDevelopment() {
	uni.showToast({
		icon: 'none',
		title: '开发中，敬请期待！'
	});
}

// 
export function openToast(msg) {
	if (msg) {
		uni.showToast({
			icon: 'none',
			title: msg
		});
	}
}



// 手机号中间四位星号
export function phoneSecret(phone) {
	return phone.substr(0, 3) + "****" + phone.substr(7);
}

// 图片上传
export function headerUpload() {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: 1, //最多可以选择的图片总数  
			sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有  
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有  
			success: function(res) {
				//启动上传等待中...  
				uni.showLoading({
					title: '上传中',
				});
				uni.uploadFile({
					url: DOMAIN_NAME + '/index.php/api/common/upload',
					name: 'file',
					filePath: res.tempFilePaths[0],
					formData: {
						// 'file': res.tempFilePaths[0]
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						"token": uni.getStorageSync('token') || ''
					},
					success: (res) => {
						uni.hideLoading()
						resolve(JSON.parse(res.data).data)
					},
					fail: (res) => {
						uni.hideLoading()
						resolve(JSON.parse(res.data).msg)
					}
				})
			},
		});
	})
}

//控制小程序中图片大小---过滤器
export function formatRichText(html) {
	let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
		match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
		return match;
	});
	newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
		match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
		return match;
	});
	newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	newContent = newContent.replace(/\<img/gi,
		'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
	return newContent;
}

//时间戳转
export function toTime(num, type = "1") {
	if (num) {
		//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		if (num.toString().length < 13) {
			num = num * 1000
		}
		let date = new Date(+num);
		let y = date.getFullYear();
		let MM = date.getMonth() + 1;
		MM = MM < 10 ? ('0' + MM) : MM; //月补0
		let d = date.getDate();
		d = d < 10 ? ('0' + d) : d; //天补0
		let h = date.getHours();
		h = h < 10 ? ('0' + h) : h; //小时补0
		let m = date.getMinutes();
		m = m < 10 ? ('0' + m) : m; //分钟补0
		let s = date.getSeconds();
		s = s < 10 ? ('0' + s) : s; //秒补0
		if (type == 1) {
			return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
		}
		if (type == 2) {
			return y + '-' + MM + '-' + d;
		}
		if (type == 3) {
			return h + ':' + m + ':' + s;
		}
	} else {
		return num
	}
}

// 年月日转时间戳
export function toTimestamp(timeDate) {
	console.log(timeDate);
	if (timeDate) {
		let Time = new Date(timeDate);
		console.log(Time);
		let timestemp = Time.getTime();
		console.log(timestemp);
		if (timestemp.toString().length < 13) {
			timestemp = timestemp / 1000
		}
		return timestemp
	} else {
		return timeDate
	}
}

// 判断是否非空
export function isNull(value, msg = '') {
	if (value == null || value == "" || value == "undefined" || value == undefined || value == "null" || JSON.stringify(
			value) === '{}' || JSON.stringify(value) === '[]') {

		openToast(msg);
		return true;
	} else {
		if (typeof(value) != 'object') {
			value = value.replace(/\s/g, "");
		}
		if (value == "") {
			openToast(msg);
			return true;
		}
		return false;
	}
}

// 轻提示
export function toast(info = {}, navigateOpt) {
	let title = info.title || ''
	let icon = info.icon || 'none'
	let endtime = info.endtime || 2000
	if (title) uni.showToast({
		title: title,
		icon: icon,
		duration: endtime
	})
	if (navigateOpt != undefined) {
		if (typeof navigateOpt == 'object') {
			let tab = navigateOpt.tab || 1,
				url = navigateOpt.url || '';
			switch (tab) {
				case 1:
					//跳转至 table
					setTimeout(function() {
						uni.switchTab({
							url: url
						})
					}, endtime);
					break;
				case 2:
					//跳转至非table页面
					setTimeout(function() {
						uni.navigateTo({
							url: url,
						})
					}, endtime);
					break;
				case 3:
					//返回上页面
					setTimeout(function() {
						uni.navigateBack({
							delta: parseInt(url),
						})
					}, endtime);
					break;
				case 4:
					//关闭当前所有页面跳转至非table页面
					setTimeout(function() {
						uni.reLaunch({
							url: url,
						})
					}, endtime);
					break;
				case 5:
					//关闭当前页面跳转至非table页面
					setTimeout(function() {
						uni.redirectTo({
							url: url,
						})
					}, endtime);
					break;
			}

		} else if (typeof navigateOpt == 'function') {
			setTimeout(function() {
				navigateOpt && navigateOpt();
			}, endtime);
		}
	}
}
